package com.system.role.web;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.system.common.util.ExtAjaxResponse;
import com.system.common.util.ExtPageable;
import com.system.permission.entity.Permission;
import com.system.permission.service.IPermissionService;
import com.system.role.entity.Role;
import com.system.role.entity.DTO.RoleQueryDTO;
import com.system.role.service.IRoleService;
import com.system.user.entity.User;
import com.system.user.service.IUserService;
@Controller
@RequestMapping("/role")
public class RoleController 
{
	@Autowired
	private IRoleService roleService;
	@Autowired
	private IPermissionService permissionService;
	@Autowired
	private IUserService userService;
	
	@RequestMapping("/saveOrUpdate")
	public @ResponseBody ExtAjaxResponse saveOrUpdate(Role role) 
	{
		if(role.getRoleId()==null) {
			try {
				roleService.saveOrUpdate(role);
				return new ExtAjaxResponse(true,"角色添加成功");
			}catch(Exception e){
				return new ExtAjaxResponse(false,"角色添加失败");}
		}else{	
			try {		
				roleService.saveOrUpdate(role);
				return new ExtAjaxResponse(true,"角色信息更新成功");
			}catch (Exception e) {
				return new ExtAjaxResponse(false,"角色信息更新失败");
			}	}
	}
	@RequestMapping("/savePer")
	public @ResponseBody ExtAjaxResponse savePer(@RequestParam Long roleId,@RequestParam(required=false) Long[] permissionId) 
	{
		Role role = roleService.findOne(roleId);
		if(permissionId!=null) {
		List<Long> lists = Arrays.asList(permissionId);
		List<Permission> permissions = permissionService.findIdsAll(lists);
		if(permissions!=null) {
			role.setPermissionLists(permissions);
			roleService.saveOrUpdate(role);
			return new ExtAjaxResponse(true,"权限分配成功");
		}else {
			return new ExtAjaxResponse(true,"权限分配失败");
		}
		}else {
			role.setPermissionLists(null);
			roleService.saveOrUpdate(role);
			return new ExtAjaxResponse(true,"权限分配成功");
		}
		
	}
	@RequestMapping("/delete")
	public @ResponseBody ExtAjaxResponse Delete(@RequestParam Long roleId) {
		try {
			Role role = roleService.findOne(roleId);
			roleService.delete(role);
			return new ExtAjaxResponse(true,"删除成功");
			
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}
	}
	@RequestMapping("/deleteRoles")
	public @ResponseBody ExtAjaxResponse Deletes(@RequestParam Long[] ids) {
		try {
			List<Long> lists = Arrays.asList(ids);
			if (null!=lists) {
				roleService.delete(lists);
			}
			return new ExtAjaxResponse(true,"删除成功");
		} catch (Exception e) {
			return new ExtAjaxResponse(false,"删除失败");
		}
	}
	@RequestMapping("/findOne")
	public @ResponseBody Role findOne(@RequestParam Long roleId) 
	{
		Role role =  roleService.findOne(roleId);
		
		return role;
	}
	
	
	@RequestMapping("/findAll")
	public @ResponseBody List<Role> findAll() 
	{
		 List<Role> lists =  roleService.findAll();
		
		return lists;
	}
	@RequestMapping("/findPage")
	public @ResponseBody Page<Role> findPage(RoleQueryDTO roleQueryDTO,ExtPageable pageable) 
	{
		Page<Role> page =  roleService.findAll(RoleQueryDTO.getSpecification(roleQueryDTO), pageable.getPageable());
		
		return page;
	}
	@RequestMapping("/findMyPer")
	public @ResponseBody List<Permission> findMyPer(String id) 
	{	
		User user = userService.findOne(id);
		Role role = user.getRole();
		List<Permission> list = role.getPermissionLists();
		
		return list;

	}
	@RequestMapping("/findPer")
	public @ResponseBody List<Permission> findPer(Long roleId) 
	{	
		Role role = roleService.findOne(roleId);
		List<Permission> list1 = role.getPermissionLists();
		List<Permission> list2 = permissionService.findAll();
		List<Permission> list3 = new ArrayList<Permission>();
		for(Permission p1 :list2) {
			for(Permission p2 :list1) {
			if(p1.getPermissionId()==p2.getPermissionId()){
				p1.setChecked(true);
				}
			}
			list3.add(p1);
		}
		return list3;
	}
}
